package com.study.talk.ui.app.group

import android.os.Bundle
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.SearchView
import androidx.recyclerview.widget.LinearLayoutManager
import com.study.talk.ui.adapter.GroupsAdapter
import com.study.talk.databinding.ActivityAddGroupBinding
import com.study.talk.utils.extras.showToast
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class AddGroupActivity : AppCompatActivity() {

    private val vm: AddGroupViewModel by viewModels()

    private val binding by lazy {
        ActivityAddGroupBinding.inflate(
            layoutInflater
        )
    }

    private val adapter by lazy { GroupsAdapter(GroupsAdapter.DETAIL) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        setSupportActionBar(binding.addGroupToolbar)
        initView()
        vm.groups.observe(this) {
            adapter.submitList(it)
        }
    }

    private fun initView() {
        binding.addGroupToolbar.setNavigationOnClickListener { finish() }
        binding.addGroupSearch.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(query: String): Boolean {
                if (query.isEmpty()) {
                    showToast("请输入搜索内容")
                    return false
                }
                vm.loadGroups(query)
                return true
            }

            override fun onQueryTextChange(newText: String): Boolean {
                return false
            }
        })
        binding.addGroupListview.layoutManager = LinearLayoutManager(this)
        binding.addGroupListview.adapter = adapter
    }
}